	package com.example.appcontact;

	import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.base.BaseActivity;
import com.example.connect.AsyncTask_ApiListener;
import com.example.model.OutProfile;
import com.example.util.Util;

	public class A006InputProfile extends BaseActivity implements OnClickListener,
			AsyncTask_ApiListener, OnItemSelectedListener {
		private static final int TYPE_UPDATE_MY_PROFILE = 72;
		private static final int TYPE_GET_MY_PROFILE = 71;
		private TextView tv_firstname,
		                 tv_lastname,
		                 tv_email,
		                 tv_phone,
		                 tv_home_phone;
		private EditText 
		                 et_firstname, 
	                     et_lastname,
	                     et_email, 
	                     et_home_phone, 
		                 et_address, 
		                 et_mobile,
		                 et_company_name,
		                 et_company_email,
		                 et_company_address,
		                 et_company_website,
		                 et_company_phone; 
				         
		private Spinner sn_gender;
		private Button  btn_birthday;
		private int mGender;
		private static final int DATE_DIALOG_ID = 0;
		private long birthday;
		private boolean isEdit = false;
        private int id;		
		private OutProfile mOutProfile;
		
		private TextView btBack;
		private TextView btNext;
		private TextView tvTitle;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			Intent intent  = this.getIntent();
			mOutProfile  = (OutProfile)intent.getSerializableExtra("_data");
			 Log.d("TAG", "id InputProfile :" + mOutProfile.getId());
			openDatabase();
			initView();
		}

		public void onStart() {
			super.onStart();
		}

		private void initView() {
			setContentView(R.layout.a006_input_profile);
			
			this.btBack = (TextView)this.findViewById(R.id.header_tv_back);
			this.btNext = (TextView)this.findViewById(R.id.header_tv_next);
			this.tvTitle  = (TextView)this.findViewById(R.id.header_tv_title);

			btBack.setVisibility(View.INVISIBLE);
			
			tv_firstname      = (TextView) findViewById(R.id.a005_tv_firstname);
			tv_lastname      = (TextView) findViewById(R.id.a005_tv_lastname);
			tv_email      = (TextView) findViewById(R.id.a005_tv_email);
			tv_phone      = (TextView) findViewById(R.id.a005_tv_mobile);
			
			
			et_home_phone      = (EditText) findViewById(R.id.a005_et_phone);
			et_address    = (EditText) this.findViewById(R.id.a005_et_address);
			et_email      = (EditText) findViewById(R.id.a005_et_email);
			et_firstname  = (EditText) findViewById(R.id.a005_et_firstname);
			et_mobile     = (EditText) findViewById(R.id.a005_et_mobile);
			et_lastname   = (EditText) findViewById(R.id.a005_et_lastname);
			btn_birthday  = (Button) this.findViewById(R.id.a005_btn_birthday);
			sn_gender     = (Spinner) this.findViewById(R.id.a005_sn_gender);
			
			et_company_name    = (EditText) this.findViewById(R.id.a005_et_company_name);
			et_company_website = (EditText) this.findViewById(R.id.a005_et_company_website);
			et_company_email = (EditText) this.findViewById(R.id.a005_et_company_email);
			et_company_phone   = (EditText) this.findViewById(R.id.a005_et_company_phone);
			et_company_address = (EditText) this.findViewById(R.id.a005_et_company_address);
			
			btn_birthday.setOnClickListener(this);
			this.btNext.setText(this.getResources().getString(R.string.done));
			this.tvTitle.setText(this.getResources().getString(R.string.update_profile));
			btNext.setOnClickListener(this);
			sn_gender.setOnItemSelectedListener(this);
			initData();
		}

		private void initData() {
			if (mOutProfile != null) {
				id = mOutProfile.getId();
				Log.e("A006-initData()","Profile at A006: " +  mOutProfile.toString());
				et_email.    setText(mOutProfile.get_email());
				et_home_phone.    setText(mOutProfile.get_home_phone());
				et_address.  setText(mOutProfile.get_home_address());
				et_mobile.  setText(mOutProfile.get_mobile());
				et_firstname.setText(mOutProfile.get_first_name());
				et_lastname. setText(mOutProfile.get_last_name());
				sn_gender.setSelection(mOutProfile.get_gender());
				
				try{
					String temp  = mOutProfile.get_birthday();
					Log.e("Temp date birthday", "date birthday get form database :" + temp);
					if(temp != null && !temp.equalsIgnoreCase("")){
						birthday = Long.parseLong(temp);
					}else{
						birthday  = new Date().getTime();
					}
					Date t  = new Date(birthday);
					String newstring = new SimpleDateFormat("dd/MM/yyyy").format(t);
					btn_birthday.setText(newstring);

					
				}catch(Exception e){}
				et_company_name.setText(mOutProfile.get_company_name());
				et_company_website.setText(mOutProfile.get_company_website());
				et_company_email.setText(mOutProfile.get_company_email());
				et_company_phone.setText(mOutProfile.get_company_phone());
				et_company_address.setText(mOutProfile.get_company_address());
			}		
			
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.a005_btn_birthday:
				Util.hideSoftKeyboard(mContext, et_email);
				showDialog(DATE_DIALOG_ID);
				break;
			case R.id.header_tv_next :
				Util.hideSoftKeyboard(mContext, et_email);
				getUpdate();
				break;
			default:
				break;
			}
		}
		@Override
		public boolean dispatchKeyEvent(KeyEvent event) {
			// TODO Auto-generated method stub
			if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
					&& event.getAction() == KeyEvent.ACTION_DOWN) {
				// nothing
			}
			return super.dispatchKeyEvent(event);
		}
		public void onBackPressed() {

		 // no thing
		    }
		
		@Override
		public Object callApi(int serachType, Object apidata) {
		
			return null;
		}

		@Override
		public void finishApi(int serachType, Object apiresult) {
			
		}

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
			this.mGender = arg2;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}

		
		private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
				Calendar  calendar  = Calendar.getInstance();
				calendar.set(year, monthOfYear, dayOfMonth);
				birthday  = calendar.getTimeInMillis();
				Date t  = new Date(birthday);
				String newstring = new SimpleDateFormat("dd/MM/yyyy").format(t);
				btn_birthday.setText(newstring);
			}
		};

		@Override
		protected Dialog onCreateDialog(int id) {
			switch (id) {
			case DATE_DIALOG_ID:
				Date t  = new Date(birthday);
				Calendar  calendar  = Calendar.getInstance();
				calendar.setTime(t);
				return new DatePickerDialog(this, mDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
			}
			return null;
		}
		private void createHashMapUpdateProfile() {
			
			try {
				final String mFirstname       = et_firstname.getText().toString();
				final String mLastname        = et_lastname.getText().toString();
				final String mEmail           = et_email.getText().toString();
				final String mPhone           = et_home_phone.getText().toString();
				final String mBirthday        = String.valueOf(birthday);
				final String mMobile          = et_mobile.getText().toString();
				final String mAddress         = et_address.getText().toString();
				final String mCompanyPhone    = et_company_phone.getText().toString();
				final String mCompanyName     = et_company_name.getText().toString();
				final String mCompanyEmail    = et_company_email.getText().toString();
				final String mCompanyAddress  = et_company_address.getText().toString();
				final String mCompanyWebsite  = et_company_website.getText().toString();
				
				mOutProfile = new OutProfile();
				mOutProfile.setId(id);
				mOutProfile.set_first_name(mFirstname);
				mOutProfile.set_last_name(mLastname);
				mOutProfile.set_home_phone(mPhone);
				mOutProfile.set_birthday(mBirthday);
				mOutProfile.set_mobile(mMobile);
				mOutProfile.set_home_address(mAddress);
				
				mOutProfile.set_gender(mGender);
				mOutProfile.set_email(mEmail);
				
				mOutProfile.set_company_address(mCompanyAddress);
				mOutProfile.set_company_phone(mCompanyPhone);
				mOutProfile.set_company_name(mCompanyName);
				mOutProfile.set_company_email(mCompanyEmail);
				mOutProfile.set_company_website(mCompanyWebsite);
				Log.e("A006 - createHashMapUpdateProfile()", " mOutProfile: " + mOutProfile.toString());
				
			} catch (Exception ex) {
				ex.printStackTrace();
			} 	
		}
		private void getUpdate() {
			boolean is_null  = false;
			      if(Util.checkNullString(et_firstname) ){
				   is_null = true;
				   tv_firstname.setTextColor(Color.RED);
			      }
			      if(Util.checkNullString(et_lastname)){
				   is_null = true;
				   tv_lastname.setTextColor(Color.RED);
			   }
			   if(Util.checkNullString(et_email) ){
				   is_null = true;
				   tv_email.setTextColor(Color.RED);
			   }
					
			   if(Util.checkNullString(et_mobile)){
				   is_null = true;
				   tv_phone.setTextColor(Color.RED);
			   }	
			   if (is_null){
				   Util.showDialog(this, R.string.warning,R.string.msg_error_empty_infor, R.drawable.exclamation);
			   }else {
				   createHashMapUpdateProfile();
				   mDB.createProfile(mOutProfile);
				   Log.e("A006 - getUpdate()", "Insert Database completed width id" +  mOutProfile.getId());
				   Intent returnIntent = new Intent();
				   setResult(RESULT_OK,returnIntent);     
				   finish();
			   }
			       
		}
		@Override
		public void tokenExpride() {
			Util.startLogin(this.getParent());

		}
	}
